<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/5
 * Time: 14:46
 */

namespace Controller;

class RbacController extends TooController {

    //角色管理
    public function role(){
        $arr=$this->pdo->pdo_sel("select * from s_role",2);
        foreach ($arr as &$v){
            $v['user']=$this->pdo->pdo_sel("select `name` from s_user where r_id = '{$v['id']}'",2);
        }
        $this->view->assign('arr',$arr);
        $this->view->display();
    }

    //添加角色---对应模板 admin-role-add.html
    public function role_add(){
        if($_POST){
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['msg']=$_POST['msg'];
            $arr['a_id']= implode('|',$_POST['a_id']);
           if($this->pdo->pdo_ins($arr,'s_role')){
               exit('插入成功');
           }else{
               exit('插入失败');
           }
        }
        //查询所有的权限,并且二级显示在一级的后面
        $arr=$this->pdo->pdo_sel("select * from s_access where pid=0",2);
        //无限极排序
        foreach ($arr as &$v){
            $v['data']=$this->pdo->pdo_sel("select * from s_access where pid='{$v['id']}'",2);
        }
        $this->view->assign('arr',$arr);
        $this->view->display();
    }

    //角色修改
    public function role_edit(){
        if($_POST){
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['msg']=$_POST['msg'];
            $arr['a_id']= implode('|',$_POST['a_id']);
            if($this->pdo->pdo_upd($arr,'s_role'," id = '{$_POST['id']}'")){
                exit('修改成功');
            }else{
                exit('修改失败');
            }
        }
        $id=$_GET['id'];
        $list=$this->pdo->pdo_sel("select * from s_role where id = $id");
        $this->view->assign('list',$list);

        //查询所有的权限,并且二级显示在一级的后面
        $arr=$this->pdo->pdo_sel("select * from s_access where pid=0",2);
        //无限极排序
        foreach ($arr as &$v){
            $v['data']=$this->pdo->pdo_sel("select * from s_access where pid='{$v['id']}'",2);
        }
        $this->view->assign('arr',$arr);
        $this->view->display('role_add');
    }


    //角色删除
    public function role_del(){
        $id=trim($_GET['id'],',');
        if($this->pdo->pdo_del('s_role'," id in ($id)")){
            $this->retuAjax(array('type'=>1));
        }else{
            $this->retuAjax(array('type'=>2));
        }
    }

    //权限管理
    public function permission(){
        //查询所有的权限,并且二级显示在一级的后面
        $arr=$this->pdo->pdo_sel("select * from s_access",2);

        //无限极排序
        $arr=$this->wxjSort($arr);
        $this->view->assign('arr',$arr);
        $this->view->display();
    }

    //添加权限
    public function permission_add(){
        if($_POST){
            //提交权限
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['pid']=$_POST['pid'];
            $arr['controll']=$_POST['controll'];
            if($this->pdo->pdo_ins($arr,'s_access')){
                exit('插入成功');
            }else{
                exit('插入错误');
            }
        }
        //查出所有的顶级节点
        $list=$this->pdo->pdo_sel("select * from s_access where pid = 0",2);
        $this->view->assign('list',$list);
        $this->view->display();
    }

    //节点修改
    public function permission_edit(){
        if($_POST){
            //提交权限
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['pid']=$_POST['pid'];
            $arr['controll']=$_POST['controll'];
            $id=$_POST['id'];
            if($this->pdo->pdo_upd($arr,'s_access'," id = '{$id}'")){
                exit('修改成功');
            }else{
                exit('修改错误');
            }
        }
        $id=$_GET['id'];
        $arr=$this->pdo->pdo_sel("select * from s_access where id=$id");
        $this->view->assign('arr',$arr);
        //查出所有的顶级节点
        $list=$this->pdo->pdo_sel("select * from s_access where pid = 0",2);
        $this->view->assign('list',$list);
        $this->view->display('permission_add');
    }


    //用户管理
    public function user(){
        $arr=$this->pdo->pdo_sel("select * from s_user",2);
        foreach ($arr as &$v){
            $id=str_replace('|',',',$v['r_id']);
            $v['role']=$this->pdo->pdo_sel("select * from s_role where id in ($id)",2);
        }
        $this->view->assign('arr',$arr);
        $this->view->display();
    }

    public function user_edit(){
        if($_POST){
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['pass']=$_POST['pass'];
            $arr['ip']=$_SERVER['REMOTE_ADDR'];
            $arr['time']=date("Y-m-d H:i:s",time());
            $arr['r_id']=implode("|",$_POST['r_id']);
            if($this->pdo->pdo_upd($arr,' s_user'," id = '{$_POST['id']}'")){
                exit('修改成功');
            }else{
                exit('修改失败');
            }
        }
        $id=$_GET['id'];
        $list=$this->pdo->pdo_sel("select * from s_user where id = $id");
        $this->view->assign('list',$list);
        //查询所有的角色
        $role=$this->pdo->pdo_sel("select * from s_role",2);
        $this->view->assign('role',$role);
        $this->view->display('user_add');
    }

    //用户删除
    public function user_del(){
        $id=trim($_GET['id'],',');
        if($this->pdo->pdo_del('s_user'," id in ($id)")){
            $this->retuAjax(array('type'=>1));
        }else{
            $this->retuAjax(array('type'=>2));
        }
    }

    //添加用户
    public function user_add(){
        if($_POST){
            //判断用户名是否存在
            $user=$this->pdo->pdo_sel("select * from s_user where name = '{$_POST['name']}'");
            if(!empty($user)){
                exit('用户名已经存在');
            }

            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['pass']=$_POST['pass'];
            $arr['ip']=$_SERVER['REMOTE_ADDR'];
            $arr['time']=date("Y-m-d H:i:s",time());
            $arr['r_id']=implode("|",$_POST['r_id']);
            if($this->pdo->pdo_ins($arr,' s_user')){
                exit('添加成功');
            }else{
                exit('添加失败');
            }
        }
        //查询所有的角色
        $role=$this->pdo->pdo_sel("select * from s_role",2);
        $this->view->assign('role',$role);
        $this->view->display();
    }

    //节点删除
    public function permission_del(){
        $id=trim($_GET['id'],',');
        if($this->pdo->pdo_del('s_access'," id in ($id)")){
            $this->retuAjax(array('type'=>1));
        }else{
            $this->retuAjax(array('type'=>2));
        }
    }


}








